﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BL.Entity;
using BL.Service;
using System.Globalization;
namespace LTO
{
    /// <summary>
    /// Interaction logic for _listbox.xaml
    /// </summary>
    public partial class _listbox : Window
    {
        tbl_NumberServer Numberservice = new tbl_NumberServer();
        tbl_PayService PayService = new tbl_PayService();
        List<ShowNumberDTO> show;
        List<ShowNumberDTO> prepaerPrint = new List<ShowNumberDTO>();
        int pageNo = 0;
        int pageSize = 20;
        Pay limit;
        decimal LimitNum = 0;
        decimal minSt = 0;
        decimal minEn = 0;
        string NumberType;
        bool IsMinToMax;
        bool IsShowOver;
        bool IsShowDummy;
        int MaxPage = 0;
        bool IsFirtsLoad = false;
        
        HotKey _hotKey_F1;
        HotKey _hotkey_F4;
        HotKey _hotKey_F3;
        HotKey _HotKey_Esc;
        HotKey _HotKey_Print;
        HotKey _HotKey_F2;
        HotKey _HotKey_DeleteNum;

        public _listbox(string numberType)
        {
            InitializeComponent();
            NumberType = numberType;
            this.Title = Gobalsource.GetHeader(numberType);
            Newload();
             _hotKey_F1 = new HotKey(Key.F1, KeyModifier.None, Newload);
             _hotkey_F4 = new HotKey(Key.F4, KeyModifier.None, OnHotKeyShowDummy);
             _hotKey_F3 = new HotKey(Key.F3, KeyModifier.None, OnHotKeyShowOverLimit);
             _HotKey_Esc = new HotKey(Key.Escape, KeyModifier.None, OnCloseWindows);
             _HotKey_Print = new HotKey(Key.F9, KeyModifier.None, PrintReport);
             _HotKey_F2 = new HotKey(Key.F2, KeyModifier.None, ShowMinToMax);
             _HotKey_DeleteNum = new HotKey(Key.Subtract, KeyModifier.None, DeleteNum);
            IsShowOver = false;
            IsShowDummy = false;
        }
        public void UnregisterDelete() {
            if (_HotKey_DeleteNum != null) {
                _HotKey_DeleteNum.Unregister();
            }
        }
        public void RegisterDelete()
        {
            if (_HotKey_DeleteNum != null)
            {
                _HotKey_DeleteNum.Register();
            }
        }
        
        public void Newload(HotKey hotKey)
        {
            Newload();
        }
        public void Newload()
        {
            IsMinToMax = true;
            IsFirtsLoad = true;
            limit = PayService.GetLimit(NumberType);

            LimitNum = limit.LimitBuy;
            minSt = limit.MinStart;
            minEn = limit.MinEnd;
         
            if (this.Owner != null)
            {
                ClientMain main = (ClientMain)this.Owner;
                main.ReloadSceen();
            }
            GetNumberShow();

            
        }
        protected void DeleteNum(HotKey h)
        {
            UnregisterDelete();
            deleteNUm deleteWin = new deleteNUm(show, NumberType);
            deleteWin.Owner = this;
            deleteWin.Show();
        }
        protected void ShowMinToMax(HotKey h)
        {
            IsFirtsLoad = false;
            IsMinToMax = !IsMinToMax;
            pageNo = 0;
            LoadData(show);
        }
        private void PrintReport(HotKey hotKey)
        {
            try
            {

                Gobalsource.PrintReport(prepaerPrint, NumberType, LimitNum);
                Newload();
            }
            catch (Exception)
            {
                
               
            }
        }
        private void OnCloseWindows(HotKey hotKey)
        {
            (Owner as ClientMain).UpdateData();
            this.Close();
        }

        private void OnHotKeyShowDummy(HotKey hotKey)
        {
            IsShowDummy = !IsShowDummy;
            if (IsShowDummy)
            {
                pageNo = 0;
            }
            LoadData(show);
        }
        private void OnHotKeyShowOverLimit(HotKey hotKey)
        {
            IsShowOver = !IsShowOver;
            if (IsShowOver)
            {
                pageNo = 0;
            }
            LoadData(show);
        }
        public void GetNumberShow()
        {
            List<NumberDTO> dbNUmber = new List<NumberDTO>();
            switch (NumberType)
            {
                case EnumLocal.NumberType.Over3:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_3Number(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.Tod3:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_3NumberTod(dbNUmber, NumberType);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Tod, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.Under3:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_3Number(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Under, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.Over2:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_2Number(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.Under2:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_2Number(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Under, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.Tod2:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_2Number(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Tod, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.TwinFront:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_TwinFront(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);
                    break;
                case EnumLocal.NumberType.TwinStretch:
                    dbNUmber = Numberservice.get_numberShow(NumberType);
                    dbNUmber = Gobalsource.GenDummy_TwinStretch(dbNUmber);
                    show = Gobalsource.ConvetNumberShow(dbNUmber, EnumLocal.NumberConvertShow.Over, LimitNum, minSt, minEn);
                    break;

            }


          //  btnCut.IsEnabled = show.Count(c => c.IsOver == true) >= 1;

           
            LoadData(show);

        }

        private void _listbox_KeyDown(object sender, KeyEventArgs e)
        {
            CheckKey(sender, e);
        }

        private List<ShowNumberDTO> LoadData(List<ShowNumberDTO> show)
        {
            List<ShowNumberDTO> temp = new List<ShowNumberDTO>();
            foreach (ShowNumberDTO item in show)
            {
                temp.Add(item.Clone());  
            }
         
            if (IsShowDummy)
            {

                temp = Gobalsource.GenDummy_3NumberTod(temp, LimitNum, minSt, minEn);
                temp = temp.Where(w=>w.IsDummy=true).ToList();
            }
          
          
            if (IsShowOver)
            {
                temp = temp.Where(s => s.IsOver == true).ToList();
                temp.ForEach(p => p.Price = p.Price - limit.LimitBuy);
            }
          
            if (IsMinToMax && !IsFirtsLoad)
            {
                temp = temp.OrderBy(n => n.Price).ToList();
              
            }
            else
            {
                if (!IsFirtsLoad)
                {
                    temp = temp.OrderByDescending(n => n.Price).ToList();
                }
               
            }
            listBoxPage1.ItemsSource = temp.Skip(pageNo + (0 * pageSize)).Take(pageSize);
            listBoxPage2.ItemsSource = temp.Skip(pageNo + (1 * pageSize)).Take(pageSize);
            listBoxPage3.ItemsSource = temp.Skip(pageNo + (2 * pageSize)).Take(pageSize);
            listBoxPage4.ItemsSource = temp.Skip(pageNo + (3 * pageSize)).Take(pageSize);
            listBoxPage5.ItemsSource = temp.Skip(pageNo + (4 * pageSize)).Take(pageSize);
            labCurrentPage.Content = string.Format("หน้า {0}/{1}", pageNo / 100 + 1, Math.Ceiling(double.Parse(temp.Count().ToString()) / 100));

            MaxPage = int.Parse(((Math.Ceiling(double.Parse(temp.Count().ToString()) / 100) - 1) * 100).ToString());
            labSum.Content = String.Format("{0:#,#}", temp.Sum(s => s.Price));
            prepaerPrint=temp;
           
        
            return temp;
        }

       



        private void CheckKey(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {

                case Key.PageDown:
                    if (pageNo > 1)
                    {
                        pageNo -= 100;
                    }
                    break;
                case Key.PageUp:
                    if (pageNo < MaxPage)
                    {
                        pageNo += 100;
                    }
                    break;
            
                case Key.OemMinus :
                    try
                    {
                        ShowNumberDTO select = (sender as ListBox).SelectedItem as ShowNumberDTO;
                        Numberservice.DeleteNumber(select, NumberType, Gobalsource.UserNameLogin);
                        this.GetNumberShow();
                    }
                    catch (Exception)
                    {


                    }

                    break;
                case Key.Subtract:
                    try
                    {
                        ShowNumberDTO select = (sender as ListBox).SelectedItem as ShowNumberDTO;
                        Numberservice.DeleteNumber(select, NumberType, Gobalsource.UserNameLogin);
                        this.GetNumberShow();
                    }
                    catch (Exception)
                    {


                    }

                    break;

            }
            LoadData(show);
        }



        private void btnCut_Click(object sender, RoutedEventArgs e)
        {
            tbl_ExportService exportService = new tbl_ExportService();
            exportService.ExportNumber(LimitNum, NumberType, Gobalsource.UserNameLogin);
            this.GetNumberShow();
        }

        private void listBoxPage1_KeyDown(object sender, KeyEventArgs e)
        {


        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                (Owner as ClientMain).UpdateData();
                this.Close();
            }
            catch (Exception)
            {


            }

        }



        private void StatusBar_KeyDown(object sender, KeyEventArgs e)
        {
            CheckKey(sender, e);
        }

        private void Window_KeyDown_1(object sender, KeyEventArgs e)
        {
            CheckKey(sender, e);
        }

        private void Grid_KeyDown(object sender, KeyEventArgs e)
        {
            CheckKey(sender, e);
        }

        private void listBoxPage1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            CheckKey(sender, e);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }



    }
    public class DummyData
    {
        public string Name { get; set; }
        public bool IsHighlighted { get; set; }
    }

}
